package com.qidian.mes.dv.controller;

import com.alibaba.excel.EasyExcel;
import com.qidian.mes.dv.service.SubjectService;
import com.qidian.mes.pojo.Machinery;
import com.qidian.mes.pojo.Subject;
import com.qidian.mes.util.Tools;
import com.qidian.mes.util.dot.PagerDTO;
import com.qidian.mes.util.pojo.QdResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author 小可怎么都睡不够
 * @Description
 * @Time 2024-04-09 15:30
 */
@CrossOrigin
@RestController
@RequestMapping("/subject")
public class SubjectController {
    @Autowired
    private SubjectService subjectService;

    @GetMapping("/export")
    public void export(Subject subject, @RequestParam(defaultValue = "1")int page, @RequestParam(defaultValue = "10")int limit, HttpServletResponse response) throws IOException {
        // 查询数据
        QdResult<PagerDTO<Subject>> qdResult = subjectService.queryByPage(subject, page, limit);
        List<Subject> subjects = qdResult.getData().getData();
        // 导出
        try {
            // 设置contentType为excel的格式
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("点检保养项目信息-" + System.currentTimeMillis(), "UTF-8").replaceAll("\\+", "%20");
            // 让文件以附件的形式下载
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
            // 这里需要设置不关闭流
            EasyExcel.write(response.getOutputStream(), Subject.class).sheet("点检保养项目信息").doWrite(subjects);
        } catch (Exception e) {
            // 重置response
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(Tools.object2json(QdResult.error("下载文件失败")));
        }
    }

    @GetMapping("/deletes")
    public QdResult deletes(Long [] subjectIds){
        return subjectService.deleteBySubjectIds(subjectIds);
    }

    @GetMapping("/delete")
    public QdResult deleteBySubjectId(Long subjectId){
        return subjectService.deleteBySubjectId(subjectId);
    }

    @PostMapping("/update")
    public QdResult updateSubject(@RequestBody Subject subject) {
        return subjectService.updateSubject(subject);
    }

    @GetMapping("/queryById")
    public QdResult queryById(Long subjectId) {
        return subjectService.queryBySubjectId(subjectId);
    }

    @RequestMapping("/save")
    public QdResult saveSubject(@RequestBody Subject subject) {
        return subjectService.saveSubject(subject);
    }

    @RequestMapping("/queryByPage") // GetMapping只能接受get请求
    public Object queryByPage(Subject subject, @RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "10") int limit) {
        return subjectService.queryByPage(subject, page, limit);
    }
}
